package com.stp.rabbitmq.listener;

import com.rabbitmq.client.Channel;
import com.stp.rabbitmq.config.DeadConfig;
import com.stp.rabbitmq.config.FanoutConfig;
import com.stp.rabbitmq.constant.QueueConstant;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;


@Component
public class DirectRabbitmqListener {



    /**
     * 路由模式  绑定交换机和队列，并为key赋值
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "DirectQueue1"),
            exchange = @Exchange(name = "DirectExchange",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println("listenDirectQueue1接收到的消息："+msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "DirectQueue2"),
            exchange = @Exchange(name = "DirectExchange",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println("listenDirectQueue2接收到的消息："+msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
